package barcan.flutter.io.barscan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.google.zxing.CaptureActivity;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;

import static android.app.Activity.RESULT_OK;

/** BarscanPlugin */
public class BarscanPlugin implements MethodCallHandler {
  private Activity activity;
  private Result result;
  private BarscanPlugin(Activity activity) {
    this.activity = activity;
  }
  /** Plugin registration. */
  public static void registerWith(Registrar registrar) {
    final MethodChannel channel = new MethodChannel(registrar.messenger(), "barscan");
    BarscanPlugin barscanPlugin=   new BarscanPlugin(registrar.activity());
    channel.setMethodCallHandler(barscanPlugin);
  }

  private void showBarcodeView(String language) {

    CaptureActivity.open(activity, language,false, true, true, new CaptureActivity.OnScanResultListener() {
      @Override
      public void onResult(String value) {
        if(result!=null)
         result.success(value);
      }
    });

  }
  @Override
  public void onMethodCall(MethodCall call, Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
      return;
    }if(call.method.equals("scan")){
      String language=  call.argument("language");
      this.result = result;
      showBarcodeView(language);
      return;
    }
  }
}
